﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProcurementManagement.Applcation.Command.ProcureApplyCommand;
using ProcurementManagement.Applcation.Command.ProcurementReturnCommand;
using ProcurementManagement.Applcation.Command.PurchaseRefundCommand;
using ProcurementManagement.Applcation.Command.PurchaseReturnsCommand;
using ProcurementManagement.Applcation.CommandHanlers.PurchaseReturnsCommandHandler;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProcurementManagement.Controllers
{
    /// <summary>
    /// 采购退款控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class PurchaseRefundController : ControllerBase
    {
        private readonly IMediator mediator;

        public PurchaseRefundController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 显示应退采购退款列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<GetPurchaseRefundDto>>> GetProcurementReturnList([FromQuery] GetPurchaseRefundCommand command)
        {
            ApiResult<PageDto<GetPurchaseRefundDto>> result = new ApiResult<PageDto<GetPurchaseRefundDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 采购退款（直接退款）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddDirectRefund(PurchaseReturnsDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddName= name;
                command.AddTime = DateTime.Now;
                command.Office_Id = sid;
                command.User_Id = Convert.ToInt32(userId);
                result.Data = await mediator.Send(new PurchaseReturnsCreateCommand { dto=command});
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示实退采购退款列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDirectRefund>> GetProcurementReturnLists([FromQuery] PurchaseReturnsSelectCommand command)
        {
            ApiResult<PageDirectRefund> result = new ApiResult<PageDirectRefund>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除实退采购款列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelDirectRefund(DelDirectRefundCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填实退采购款列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<DirectRefund>> FanDirectRefund([FromQuery] FanDirectRefundCommand command)
        {
            ApiResult<DirectRefund> result = new ApiResult<DirectRefund>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                //command.AddName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改实退采购款列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdDirectRefund(UpdDirectRefundDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.UpdName = name;
                command.UpdTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                result.Data = await mediator.Send(new UpdDirectRefundCommand { dto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }














    }
}
